package com.css.fxfzypg.modules.actionTrack.repository.impl;

import com.css.fxfzypg.modules.actionTrack.entity.ActionTrackEntity;
import com.css.fxfzypg.modules.actionTrack.repository.ActionTrackRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/27 10:19
 **/
@Repository
public class ActionTrackRepositoryImpl implements ActionTrackRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(ActionTrackEntity actionTrackEntity) {
        String sql="insert into ypg_action_track values(:footId, :userId, :longitude, :latitude, :remark, :createTime, :batch, :phasePoint)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(actionTrackEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }


    @Override
    public List<ActionTrackEntity> getActionTracks(String userId,String startTime,String endTime) {
       String sql="select * from ypg_action_track where user_id= ? and create_time >= ? and create_time <= ? ORDER BY create_time ASC";
        List<ActionTrackEntity> actionTrack = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ActionTrackEntity.class),userId,Timestamp.valueOf(startTime),Timestamp.valueOf(endTime));
        return actionTrack;
    }

    @Override
    public ActionTrackEntity fingByFootId(String footId) {
        String sql="select * from ypg_action_track where foot_id = ?";
        ActionTrackEntity actionTrack = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ActionTrackEntity.class),footId);
        return actionTrack;
    }
}
